<?php

namespace app\erp\model;

use think\Model;
use think\Cache;

class CategoryBranddb extends Model {
	protected $name = 'category_brand';
	protected $type = [ 
			'create_time' => 'timestamp:Y-m-d' 
	];
	// 自动完成时间转换
	protected $autoWriteTimestamp = true;
	protected $insert = [ 
			'site_id',
			'admin_id' => MEMBER_ID 
	];
	protected function setSiteIdAttr($value) {
		if (! $value) {
			return SITE_ID;
		}
		if ($value == 0) {
			return null;
		}
		return $value;
	}
	public $param = array ();
	protected function cache_name($name) {
		$new_arr = $this->param;
		$new_arr ['partner_id'] = PARTNER_ID;
		if (! isset ( $new_arr ['site_id'] )) {
			$new_arr ['site_id'] = SITE_ID;
		}
		ksort ( $new_arr );
		$param_str = http_build_query ( $new_arr );
		return md5 ( $name . $param_str );
	}
	// 分类查询
	public function scopeCategorySign($query, $categorySign) {
		$query->where ( 'category_sign', 'IN', $categorySign );
	}
	// 基础查询
	public function base($query) {
		if (isset ( $this->param ['site_id'] )) {
			if ($this->param ['site_id'] == 0 || $this->param ['site_id'] == '') {
				$query->where ( 'site_id', 'null' );
			} else {
				$query->where ( 'site_id', $this->param ['site_id'] );
			}
		} else {
			if (! empty ( SITE_ID )) {
				$query->where ( 'site_id', SITE_ID );
			} else {
				$query->where ( 'site_id', 'null' );
			}
		}
		if (isset ( $this->param ['category_sign'] ) && ! empty ( $this->param ['category_sign'] )) {
			$query->categorySign ( $this->param ['category_sign'] );
		} else {
			$query->categorySign ( 0 );
		}
		if (isset ( $this->param ['brand_sign'] ) && $this->param ['brand_sign']) {
			$query->brandSign ( $this->param ['brand_sign'] );
		}
	}
	public function scopeBrandSign($query, $brand_sign) {
		$query->where ( 'brand_sign', $brand_sign );
	}
	public function ajaxFind() {
		$data = $this->find ();
		if ($data) {
			return $data->toArray ();
		} else {
			return array ();
		}
	}
	
	// 同步分类品牌
	public function copy() {
		$data = $this->cache ( $this->cache_name ( 'category_brand_copy' ), 86400, 'category_brand' )->select ();
		if ($data) {
			$data = $data->toArray ();
			$add_data = array ();
			if ($this->param ['select_site_id'] == '') {
				$this->param ['site_id'] = SITE_ID;
			} else {
				$this->param ['site_id'] = $this->param ['select_site_id'];
			}
			foreach ( $data as $key => $val ) {
				unset ( $val ['category_brand_id'] );
				unset ( $val ['create_time'] );
				unset ( $val ['admin_id'] );
				$val ['site_id'] = $this->param ['site_id'];
				$add_data [$key] = $val;
			}
			
			$this->ajaxDelete ();
			$this->saveAll ( $add_data );
		}
		return 'success';
	}
	// 查询
	public function ajaxSelect() {
		$return ['data'] = $this->ajaxList ();
		$return ['count'] = $this->ajaxCount ();
		return $return;
	}
	public function ajaxList() {
		$data = $this->cache ( $this->cache_name ( 'category_brand_list' ), 86400, 'category_brand' )->order ( 'create_time desc' )->select ();
		
		if (! $data->isEmpty ()) {
			$data = $data->toArray ();
		}
		return $data;
	}
	public function ajaxCount() {
		$count = $this->cache ( $this->cache_name ( 'category_brand_count' ), 86400, 'category_brand' )->count ();
		return $count;
	}
	// 保存
	public function ajaxSave() {
		$this->allowField ( true )->save ( $this->param );
		Cache::clear ( 'category_brand' );
		Cache::clear ( 'brand' );
		return 'success';
	}
	// 设置新增分类下的品牌
	public function addCategoryBrand($category_sign) {
		$this->save ( [ 
				'category_sign' => $category_sign 
		], [ 
				'admin_id' => session ( 'member_id' ),
				'category_sign' => '0' 
		] );
		Cache::clear ( 'category_brand' );
		Cache::clear ( 'brand' );
		return 'success';
	}
	// 删除
	public function ajaxDelete() {
		if (isset ( $this->param ['category_sign'] ) && isset ( $this->param ['brand_sign'] )) {
			$this->where ( 'category_sign', $this->param ['category_sign'] )->where ( 'brand_sign', $this->param ['brand_sign'] )->delete ();
			Cache::clear ( 'category_brand' );
			Cache::clear ( 'brand' );
		}
	}
}